﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using GPSTracker.Helper;

namespace GPSTracker.Views.DAL
{
    public class LoginDAL
    {
        public DataTable FillLoginMasterGrid(string strLoginID)
        {
            var sb = new StringBuilder();
            var dt = new DataTable();
            if (strLoginID.Trim() == string.Empty)
            {
                var strSQL = "SELECT * FROM VW_GetLoginMaster WHERE IsActive = 1";
                var da = new SqlDataAdapter(strSQL, CommonFx.GetSqlCon());
                da.Fill(dt);
            }
            else
            {
                sb.Append("SELECT * FROM VW_GetLoginMaster WHERE LoginID = '" + strLoginID + "' ");
                var da = new SqlDataAdapter(sb.ToString(), CommonFx.GetSqlCon());
                da.Fill(dt);
            }

            return dt;
        }

        public DataTable FillDesignation(string strAttributeID)
        {
            var sb = new StringBuilder();
            var dt = new DataTable();

            if (strAttributeID.Trim() == string.Empty)
            {
                var strSQL = "SELECT * FROM VW_GetDesignation where IsActive = 1";
                var da = new SqlDataAdapter(strSQL, CommonFx.GetSqlCon());
                da.Fill(dt);
            }
            else
            {
                sb.Append("SELECT * FROM VW_GetDesignation where IsActive = 1 ");
                sb.Append("UNION ");
                sb.Append("SELECT * FROM VW_GetDesignation where AttributeID = '" + strAttributeID + "' ");
                var da = new SqlDataAdapter(sb.ToString(), CommonFx.GetSqlCon());
                da.Fill(dt);
            }

            return dt;
        }

        public DataTable FillBranch(string strBranchID)
        {
            var sb = new StringBuilder();
            var dt = new DataTable();

            if (strBranchID == string.Empty)
            {
                var strSQL = "SELECT * FROM VW_GetBranch";
                var da = new SqlDataAdapter(strSQL, CommonFx.GetSqlCon());
                da.Fill(dt);
            }
            else
            {
                var strSQL = "SELECT * FROM VW_GetBranch";
                var da = new SqlDataAdapter(strSQL, CommonFx.GetSqlCon());
                da.Fill(dt);
            }

            return dt;
        }

        public DataTable FillWarehouse(string strWarehouseID)
        {
            var sb = new StringBuilder();
            var dt = new DataTable();

            if (strWarehouseID == string.Empty)
            {
                var strSQL = "SELECT * FROM VW_GetWarehouse";
                var da = new SqlDataAdapter(strSQL, CommonFx.GetSqlCon());
                da.Fill(dt);
            }
            else
            {
                var strSQL = "SELECT * FROM VW_GetWarehouse";
                var da = new SqlDataAdapter(strSQL, CommonFx.GetSqlCon());
                da.Fill(dt);
            }

            return dt;
        }

        public void DeleteUserMaster(string strLoginID)
        {
            var sb = new StringBuilder();
            sb.Append("UPDATE dbo.M_Login SET IsActive = 0 WHERE LoginID = '" + strLoginID + "' ");

            var cmd = new SqlCommand(sb.ToString(), CommonFx.GetSqlCon());
            cmd.ExecuteNonQuery();
        }
    }
}
